package com.easyadmin.pro.tool;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.easyadmin.pro.common.constants.RedisConstants;
import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.modules.sys.entity.DataDictionaryEntity;
import com.easyadmin.pro.modules.sys.service.IDataDictionaryService;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * packageName com.easyadmin.pro.tool
 *
 * @author 骑着蚂蚁去上天
 * @version JDK 17
 * @className DataDictionaryUtils
 * @date 2024/8/26
 * @description 数据集获取对象
 */
public class DataDictionaryUtils {

    /**
     * 获取数据集Map集合
     * @param code
     * @return
     */
    public static Map<String, Object> getDataDicMap(String code) {
        Map<String, List<DataDictionaryEntity>> map = dynamicDataDicMap();
        List<DataDictionaryEntity> dataDicInfos = map.get(code);
        Map<String, Object> resultMap = new HashMap<>(dataDicInfos.size());
        dataDicInfos.stream().forEach(dataDicInfo -> resultMap
                .put(dataDicInfo.getDataValue(), dataDicInfo.getDataValueName()));
        return resultMap;
    }

    /**
     * 获取全部缓存数据
     * @return
     */
    public static Map<String, List<DataDictionaryEntity>> getDataDicAllMap() {
        return dynamicDataDicMap();
    }

    /**
     * 获取集合
     * @param code
     * @return
     */
    public static List<DataDictionaryEntity> getDataDicInfos(String code) {
        Map<String, List<DataDictionaryEntity>> map = dynamicDataDicMap();
        return map.get(code);
    }

    /**
     * 获取数据集名称
     * @param code
     * @param val
     * @return
     */
    public static String getDataDicName(String code, String val) {
        String dataDicName = null;
        List<DataDictionaryEntity> dataDicInfos = getDataDicInfos(code);
        if (!CollectionUtils.isEmpty(dataDicInfos)) {
            DataDictionaryEntity dictionary = dataDicInfos.get(0);
            dataDicName = dictionary.getDataName();
        }
        return dataDicName;
    }

    /**
     * 获取数据集内容
     * @param code
     * @param val
     * @return
     */
    public static Object getDataDicVal(String code, String val) {
        Map<String, Object> map = getDataDicMap(code);
        return map.get(val);
    }

    /**
     * 刷新数据集缓存
     */
    public static void refreshCache() {
        IDataDictionaryService dataDictionaryService = SpringUtil.getBean(IDataDictionaryService.class);
        List<DataDictionaryEntity> dataInfos = dataDictionaryService.list(new QueryWrapper()
                .eq(DataDictionaryEntity::getStatus, Integer.parseInt(StatusConstants.STATUS_1)));

        if (!CollectionUtils.isEmpty(dataInfos)) {
            Map<String, List<DataDictionaryEntity>> dataInfoMap = dataInfos.stream()
                    .collect(Collectors.groupingBy(DataDictionaryEntity::getDataCode));
            RedisUtils.refreshObj(RedisConstants.DATA_DICTIONARY_KEY, dataInfoMap);
        }

    }

    /**
     * 获取数据集集合信息
     * @param code
     * @return
     */
    public static List<Map<String, Object>> getDataDicTree(String code) {
        Map<String, Object> dataDicMap = getDataDicMap(code);
        List<Map<String, Object>> resultMap = new ArrayList<>();
        if (Objects.nonNull(dataDicMap)) {
            dataDicMap.forEach((k, b) -> {
                Map<String, Object> map = new HashMap<>(2);
                map.put("value", k);
                map.put("label", b);
                resultMap.add(map);
            });
        }
        return resultMap;
    }

    /**
     * 获取数据集Map对象
     * @return
     */
    private static Map<String, List<DataDictionaryEntity>> dynamicDataDicMap() {
        Object value = RedisUtils.getValue(RedisConstants.DATA_DICTIONARY_KEY);
        Map<String, List<DataDictionaryEntity>> resultMap = new HashMap<>(1);
        JSONObject jsonObject = JSONObject.parseObject(value.toString());
        jsonObject.forEach((k, v) -> {
            JSONArray jsonArray = JSONObject.parseArray(v.toString());
            List<DataDictionaryEntity> dataInfos = jsonArray.toJavaList(DataDictionaryEntity.class);
            resultMap.put(k, dataInfos);
        });
        return resultMap;
    }
}